#ifndef _COMMON_H__
#define _COMMON_H__

#define MEM_4KB (0x1000)
#define MEM_4MB (0x400000)

#define MEM_1KB (1024)
#define MEM_1MB (0x100000)

#define PG_ALIGN(addr) ((uptr)(addr)&0xFFFFF000UL)

#define PG_MAX_ENTRIES 1024U
#define PG_LAST_TABLE PG_MAX_ENTRIES - 1
#define PG_FIRST_TABLE 0
#define PG_SIZE_BITS 12
#define PG_SIZE (1 << PG_SIZE_BITS)
#define PG_INDEX_BITS 10

#define VGA_BUF_PA 0xB8000
#define VGA_LINE_NUM 25
#define VGA_LINE_LENGTH 80
#define VGA_BUF_SIZE 4096

#ifndef __ASM__
#include <type.h>

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef container_of
/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:	the pointer to the member.
 * @type:	the type of the container struct this is embedded in.
 * @member:	the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({			\
	const typeof(((type *)0)->member) * __mptr = (ptr);	\
	(type *)((char *)__mptr - offsetof(type, member)); })
#endif

#define wait_until(cond)                                                       \
    while (!(cond));

#define loop_until(cond)                                                       \
    while (!(cond));

#define wait_until_expire(cond, max)                                           \
    ({                                                                         \
        unsigned int __wcounter__ = (max);                                     \
        while (!(cond) && __wcounter__-- > 1)                                  \
            ;                                                                  \
        __wcounter__;                                                          \
    })

#endif

#endif /* __COMMON_H__ */